/**
 * 
 */
package algorithm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import comparators.AppointmentComparatorLength;

import entities.Appointment;
import exceptions.IllegalAppointmentException;
import exceptions.KeinFreierTermin;
import fachlogik.Administration;

/**
 * @author veit
 * 
 */
public class ShortestAppointmentFirst implements AlgorithmFace {

private Administration    admin;
/**
 * @throws IllegalAppointmentException 
 * @throws KeinFreierTermin 
 * 
 */
public ShortestAppointmentFirst(Date von, Date bis, List<Appointment> appointmentsToReschedule) throws IllegalAppointmentException, KeinFreierTermin {
    admin = Administration.getInstance();
    rescheduleAppointments(von, bis, appointmentsToReschedule);
}

/**
 * Soll soviel Termine wie möglich in das nächste Zeitloch stopfen
 * @throws IllegalAppointmentException 
 * @throws KeinFreierTermin 
 * 
 */
/*
 * @Override public boolean rescheduleCalender(Calendar c) { // TODO
 * Auto-generated method stub return false; }
 */

@SuppressWarnings("unused")
@Override
public boolean rescheduleAppointments(Date von, Date bis, List<Appointment> appointmentsToReschedule) throws IllegalAppointmentException, KeinFreierTermin {
    List<Appointment> atrL = appointmentsToReschedule;
    Collections.sort(atrL, new AppointmentComparatorLength());
    /*
     * List<Appointment> allAppL = new ArrayList(); for (Appointment a :
     * Administration.getInstance().getAllAppointments()){ allAppL.add(a); }
     * for(Appointment a : atrL) { for(Appointment b :
     * Administration.getInstance().getAllAppointments()){ if (b == a) {
     * allAppL.remove(a); } } }
     */
    FinalTool tool = new FinalTool();
    for (int i = 0; i < atrL.size(); i++) {
        Appointment app = atrL.get(i);
        Date gap = tool.findGap(app, von, bis);
        long laenge = app.getLength();
        app.setBegin(gap);
        app.setEnd(new Date(gap.getTime() + laenge));
        admin.addOrEditAppointment(app);
        return true;
    }
    return false;
}

}
